<?php

	require "classes/bdd.class.php";
	session_start();
	
	//$res = BDD::query("INSERT INTO employe SET LOGIN='COUCOU'");
	//echo $res;
	//echo "<br/>";
	//print_r($res);
	
	
//	function demo($un,$deux) {
//		echo $un . "--------------------------------------------------------------------------------------";
//		echo $deux . "</br>";
//		$res = BDD::query($deux);
//		echo "Resultats :" . "<br/>";
//		echo "<pre>";
//		print_r($res);
//		echo "</pre>";
//		echo "----------------------------------------------------------------------------------------------------------------------------------------";
//		
//		echo "<br/>";
//		echo "<br/>";
//		echo "<br/>";
//		
//	}
//	
//	demo("Select * ","select * from employe");
//	demo("Select avec erreur","select * from empo");
//	demo("Insert","INSERT into employe SET Login='coucou'");
//	demo("Insert erreur ","INSERT into employe SET Logn='coucou'");
//	demo("Update","update employe set login='coucou2' where Login='coucou'");
//	demo("Update erreur","update employe set login='coucou2' whee Login='coucou'");
//	demo("Delete","DELETE from Employe where login='coucou2'");
//	
//	echo "Exemple de traitement des resultats (voir le code) : ";
//	echo "<br/>" . "3 (select *)- sur mega multiple (Ici on n'affiche que le login, mais lacces aux autres champs se fait de la meme facon." . "<br/>";
//	$res = BDD::query("select * from employe");
//	foreach ($res as $K => $V) 
//	{
//		echo $K . "=>" . $V["LOGIN"] . "<br/>";
//		echo $K . "=>" . $V["EMAIL"] . "<br/>";
//	}
//	
//	echo "<br/>" . "2 surmultiples resultats (select distinct login" . "<br/>";
//	$res = BDD::query("select distinct LOGIN from employe");
//	echo ">>><br/>";
//	foreach ($res as $K => $V) 
//	{
//		echo $V["LOGIN"] . "<br/>";
//	}
//	echo "<<<<br/>";
//	echo "<br/>" . "1 sur resultat unique (select count(*) " . "<br/>";
//	$res = BDD::query("select count(*) from employe");
//	echo ">>>" . $res  . "<<<";
//	echo "<br/>" . "1 sur resultat unique (select LOGIN where ID = 1" . "<br/>";
//	$res = BDD::query("select LOGIN from employe where ID_EMPLOYE=1");
//	echo ">>>" . $res  . "<<<";

//$res = BDD::query ("select NOM from fournisseur where ID_FOURNISSEUR = 16",0);
//echo "<pre>";
//print_r($res);
//echo "</pre>";
//echo "COUCOU";
//$res = BDD::query ("select count(*) from employe ",3);
//echo "<pre>";
//print_r($res);
//echo "</pre>";
	
	if(isset($_POST['username']) and isset($_POST['password'])){
		try{
			$res = BDD::query('SELECT ID_EMPLOYE,NOM,PRENOM FROM employe WHERE LOGIN = \'' . $_POST['username'] . '\' and PASS = \'' . md5($_POST['password']) . '\'',2);
			if(!empty($res))
			{
				$_SESSION['ID'] = $res["ID_EMPLOYE"];
				$_SESSION['NOM'] = $res["NOM"];
				$_SESSION['PRENOM'] = $res["PRENOM"];
				$_SESSION['LOGIN'] = $_POST['username'];
				header('Location: ../index.php');
			}
			else
			{
				header('Location: ../index.php?e=1');
			}
		}			
		catch (Exception $e)
		{
			die('Erreur : ' . $e->getMessage());
		}
	}
	else
	{
		header('Location: ../index.php?e=2	');
	}
?>